<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="线程名称" prop="threadname">
        <el-input
          v-model="queryParams.threadname"
          placeholder="请输入线程名称"
          style="width: 240px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择执行状态" style="width: 240px" clearable>
          <el-option
            v-for="dict in dict.type.gg_job_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" 
     :data="threadlogList" 
     @selection-change="handleSelectionChange" 
     height="400px"
     >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="55" align="center">
        <template slot-scope="scope">
          <span>{{ (pageParams.current-1)*pageParams.size + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="线程名称" align="center" prop="threadname" />
      <el-table-column label="执行状态" width="85" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gg_job_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="kssj" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.kssj, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="jssj" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jssj, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="线程TraceId" align="center" prop="threadid" />
      <el-table-column label="错误信息" align="center" prop="cwxx" width="280" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageParams.current"
      :limit.sync="pageParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改线程运行记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listThreadlog } from "@/api/job/threadlog";

export default {
  name: "Threadlog",
  dicts: ['gg_job_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 线程运行记录表格数据
      threadlogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 线程TraceId时间范围
      daterangeKssj: [],
      // 分页参数
      pageParams: {
        current: 1,
        size: 10,
      },
      // 查询参数
      queryParams: {
        pch: null,
        threadname: null,
        status: null,
        kssj: null,
        beginKssj: null,
        endKssj: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        threadname: [
          { required: true, message: "线程名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "执行状态不能为空", trigger: "change" }
        ],
        kssj: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const pch = this.$route.query && this.$route.query.pch;
    this.queryParams.pch = pch;
    this.getList();
  },
  methods: {
    /** 查询线程运行记录列表 */
    getList() {
      this.loading = true;
      if (null != this.daterangeKssj && '' != this.daterangeKssj) {
        this.queryParams["beginKssj"] = this.daterangeKssj[0];
        this.queryParams["endKssj"] = this.daterangeKssj[1];
      } else {
        this.queryParams["beginKssj"] = null;
        this.queryParams["endKssj"] = null;
      }
      listThreadlog(this.pageParams,this.queryParams).then(response => {
        this.threadlogList = response.page.records;
        this.total = response.page.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pch: null,
        threadname: null,
        status: null,
        kssj: null,
        jssj: null,
        cwxx: null,
        threadid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeKssj = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加线程运行记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      var idUpdate = row.id;
      if(row.isTrusted){
        idUpdate = this.ids[0];
      }
      this.queryParams["id"] = idUpdate;
      getThreadlog(this.queryParams).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改线程运行记录";
        this.queryParams["id"] = null;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateThreadlog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addThreadlog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      var ids = this.ids;
      if(row.id){
        ids = new Array();
        ids.push(row.id);
      }
      this.$modal.confirm('是否确认删除选定的数据项？').then(function() {
        return delThreadlog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('job/threadlog/export', {
        ...this.queryParams
      }, `threadlog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
